#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
const int MAXSIZE = 512;
char buf[MAXSIZE], *ptr = buf;
struct WORD{
    string content;
    int type_num;
}word;
map<string, int>table;


bool is_letter() {
    return *ptr >= 'a' && *ptr <= 'z';
}


bool is_digit() {
    return *ptr >= '0' && *ptr <= '9';
}


int find_token(string& token) {
    if (table.count(token)) {
        return table[token];
    }
    return 10;
}


void scan() {
    word.type_num = 10;
    word.content = "";
    while (*ptr == ' ' || *ptr == '\n' || *ptr == '\t') {
        ++ptr;
    }
    if (is_letter()) {
        while (is_letter() || is_digit()) {
            word.content += *ptr;
            ++ptr;
        }
        word.type_num = find_token(word.content);
        return;
    }
    if (is_digit()) {
        while (is_digit()) {
            word.content += *ptr;
            ++ptr;
        }
        word.type_num = 11;
        return;
    }
    switch (*ptr) {
        case '+' :
            word.type_num = 13;
            word.content += *ptr;
            ++ptr;
            break;
        case '-' :
            word.type_num = 14;
            word.content += *ptr;
            ++ptr;
            break;
        case '*' :
            word.type_num = 15;
            word.content += *ptr;
            ++ptr;
            break;
        case '/' :
            word.type_num = 16;
            word.content += *ptr;
            ++ptr;
            break;
        case ':' :
            word.type_num = 17;
            word.content += *ptr;
            ++ptr;
            if (*ptr == '=') {
                word.content += *ptr;
                word.type_num = 18;
                ++ptr;
            }
            break;
        case '<' :
            word.type_num = 20;
            word.content += *ptr;
            ++ptr;
            if (*ptr == '>') {
                word.content += *ptr;
                word.type_num = 21;
                ++ptr;
            } else if(*ptr == '=') {
                word.content += *ptr;
                word.type_num = 22;
                ++ptr;
            }
            break;
        case '>' :
            word.type_num = 23;
            word.content += *ptr;
            ++ptr;
            if (*ptr == '=') {
                word.content += *ptr;
                word.type_num = 24;
                ++ptr;
            }
            break;
        case '=' :
            word.type_num = 25;
            word.content += *ptr;
            ++ptr;
            break;
        case ';' :
            word.type_num = 26;
            word.content += *ptr;
            ++ptr;
            break;
        case '(' :
            word.type_num = 27;
            word.content += *ptr;
            ++ptr;
            break;
        case ')' :
            word.type_num = 28;
            word.content += *ptr;
            ++ptr;
            break;
        case EOF :
            word.type_num = 0;
            break;
        default:
            word.type_num = -1;
            word.content = "ERROR";
            ++ptr;
    }
}


int main() {
    table.insert({"begin", 1});
    table.insert({"if", 2});
    table.insert({"then", 3});
    table.insert({"while", 4});
    table.insert({"do", 5});
    table.insert({"end", 6});
    FILE* input = fopen("input.txt","r");
    FILE* output = fopen("LexicalAnalysisResult.txt","w");

    int _size = fread(buf, 1, MAXSIZE, input);
    buf[_size] = EOF;
    word.type_num = -1;
    while ((*ptr) != EOF) {
        scan();
        if (word.type_num != 0) {
            fprintf(output, "%c", word.type_num);
            //fprintf(output, "(%s, %d)\n", word.content.c_str(), word.type_num);
        }
    }
    fprintf(output, "%c", 0);
    fclose(input);
    fclose(output);
    return 0;
}

